/** 1
 * This Class encapsulates a number of methods that are used in most of the TUI Layer Classes,
 * improving efficiency and overall perfomance.
 * 
 */

package TUI;

import java.util.Scanner;
import java.util.InputMismatchException;

public class InputUI
{
    private Scanner keyboard;

    /**
     *Constructor
     */
    public InputUI()
    {
        keyboard = new Scanner(System.in);
    }

    /**
     *
     * @return
     */
    public int inputEmployeeID()
    {
        boolean succeed = false;
        int itemID = 0;
        while(!succeed)
        {
            // makes an object keyboard to have input from the console

            System.out.println ("Enter the employee ID:");
            try
            {
                itemID = keyboard.nextInt();
                keyboard.nextLine();
                if(itemID < 1)
                {
                    throw new IllegalArgumentException("x < 1");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: Please enter a number greater than 0");
                keyboard.nextLine();
            }
        }//end while

        return itemID;
    }

    /**
     *
     * @return
     */
    public int inputCustomerID()
    {
        boolean succeed = false;
        int itemID = 0;
        while(!succeed)
        {
            // makes an object keyboard to have input from the console
            System.out.println ("Enter Customer ID");
            try
            {
                itemID = keyboard.nextInt();
                keyboard.nextLine();
                if(itemID < 1)
                {
                    throw new IllegalArgumentException("x < 1");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: Please enter a number greater than 0");
                keyboard.nextLine();
            }
        }//end while

        return itemID;
    }

    /**
     *
     * @return
     */
    public int inputItemID()
    {
        boolean succeed = false;
        int itemID = 0;
        while(!succeed)
        {
            // makes an object keyboard to have input from the console

            System.out.println ("Enter product ID");
            try
            {
                itemID = keyboard.nextInt();
                keyboard.nextLine();
                if(itemID < 1)
                {
                    throw new IllegalArgumentException("x < 1");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: Please enter a number greater than 0");
                keyboard.nextLine();
            }
        }//end while

        return itemID;
    }

    public int inputItemQuantity()
    {
        boolean succeed = false;
        int quantity = 0;
        while(!succeed)
        {
            try
            {
                System.out.println ("Enter number of items:");
                quantity = keyboard.nextInt();
                keyboard.nextLine();
                if(quantity < 0)
                {
                    throw new IllegalArgumentException("x < 0");
                }
                succeed = true;
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: number must be 0 or more");
                keyboard.nextLine();
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: An number is required");
                keyboard.nextLine();
            }
        }
        return quantity;
    }

    /**
     *
     * @return
     */
    public int inputUpdateItemQuantity()
    {
        boolean succeed = false;
        int quantity = 0;
        while(!succeed)
        {
            try
            {
                System.out.println ("Enter the number of items to be added:");
                quantity = keyboard.nextInt();
                keyboard.nextLine();
                if(quantity < 0)
                {
                    throw new IllegalArgumentException("x < 0");
                }
                succeed = true;
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: number must be 0 or more");
                keyboard.nextLine ();
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine ();
            }
        }
        return quantity;
    }

    /**
     *
     * @return
     */
    public int inputID()
    {
        boolean succeed = false;
        int itemID = 0;
        while(!succeed)
        {
            // makes an object keyboard to have input from the console
            System.out.println ("Enter ID");
            try
            {
                itemID = keyboard.nextInt();
                keyboard.nextLine();
                if(itemID < 1)
                {
                    throw new IllegalArgumentException("x < 1");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: Please enter a number greater than 0");
                keyboard.nextLine();
            }
        }//end while

        return itemID;
    }

    /**
     *
     * @return
     */
    public String inputItemName()
    {
        System.out.println ("Enter name:");
        String itemName = keyboard.nextLine();
        return itemName;
    }

    /**
     *
     * @return
     */
    public int inputSerialNo()
    {
        // creates an object keyboard to read data from the keyboard;
        System.out.println ("Enter the serial number:");
        int serialNo = keyboard.nextInt();
        keyboard.nextLine();
        return serialNo;
    }

    /**
     *
     * @return
     */
    public double inputDiscount()
    {
        // creates an object keyboard to read data from the keyboard;
        System.out.println ("Enter Discount");
        double discount = keyboard.nextDouble();
        return discount;
    }

    /**
     *
     * @return
     */
    public String inputDescription()
    {
        // creates an object keyboard to read data from the keyboard
        System.out.println ("Enter description");
        String description = keyboard.nextLine();
        return description;
    }

    /**
     *
     * @return
     */
    public double inputItemPrice()
    {
        boolean succeed = false;
        double itemPrice = 0;
        while(!succeed)
        {
            try
            {
                System.out.println ("Enter item price:");
                itemPrice = keyboard.nextDouble();
                keyboard.nextLine();
                if(itemPrice <= 0)
                {
                    throw new IllegalArgumentException("x <= 0");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: The price must be above 0");
            }

        }
        return itemPrice;
    }

    /**
     *
     * @return
     */
    public int inputMaxInStock()
    {
        // creates an object keyboard to read data from the keyboard
        boolean succeed = false;
        int maxInStock = 0;
        while(!succeed)
        {
            try
            {
                System.out.println ("Enter the maximum inventory:");
                maxInStock = keyboard.nextInt();
                keyboard.nextLine();
                if(maxInStock < 1)
                {
                    throw new IllegalArgumentException("x < 1");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: The number must be at least 1");
                keyboard.nextLine();
            }
        }
        return maxInStock;
    }

    /**
     *
     * @return
     */
    public int inputMinInStock()
    {
        // creates an object keyboard to read data from the keyboard
        boolean succeed = false;
        int minInStock = 0;
        while(!succeed)
        {
            try
            {
                System.out.println ("Enter the amount held:");
                minInStock = keyboard.nextInt();
                keyboard.nextLine();
                if(minInStock < 0)
                {
                    throw new IllegalArgumentException("x < 0");
                }
                succeed = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: The number must be less than 0");
                keyboard.nextLine();
            }
        }

        return minInStock;
    }

    /**
     *
     * @return
     */
    public int inputItemsInStock()
    {
        // creates an object keyboard to read data from the keyboard
        boolean succeed = false;
        int itemsInStock = 0;
        while(!succeed)
        {
            try
            {
                System.out.println ("Enter number:");
                itemsInStock = keyboard.nextInt();
                keyboard.nextLine();
                if(itemsInStock < 0)
                {
                    throw new IllegalArgumentException("x < 0");
                }
                succeed = true;
            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("Error: The number must be greater than 0");
                keyboard.nextLine();
            }
            catch(InputMismatchException e)
            {
                System.out.println ("Error: A number is required");
                keyboard.nextLine();
            }
        }
        return itemsInStock;
    }

    /**
     *
     * @return
     */
    public String inputType()
    {
        // creates an object keyboard to read data from the keyboard
        System.out.println ("Enter the type:");
        String type = keyboard.nextLine();
        return type;
    }

    /**
     *
     * @return
     */
    public String inputBrand()
    {
        // creates an object keyboard to read data from the keyboard
        System.out.println("Enter Brand: ");
        String brand = keyboard.nextLine();
        return brand;
    }

    /**
     *
     * @return
     */
    public String inputPlace()
    {
        // creates an object keyboard to read data from the keyboard
        System.out.println("Enter location: ");
        String place = keyboard.nextLine();
        return place;
    }

    public void pause()
    {
        System.out.println ("Press Enter to continue");
        keyboard.nextLine();
        //       keyboard.nextLine(); //Tilføjet, da den forrige nogle gange springes over
    }

    /**
     *
     * @return
     */
    public boolean yesOrNo()
    {
        boolean isYes = false;
        boolean succeed = false;
        while(!succeed)
        {
            System.out.println ("Are you sure? (Y / n)");
            String input = keyboard.nextLine();
            if(input.equals("y"))
            {
                isYes = true;
                succeed = true;

            }
            if(input.equals("n"))
            {
                isYes = false;
                succeed = true;
            }
        }
        return isYes;
    }

    /**
     *
     * @return
     */
    public boolean UnitYesOrNo()
    {
        boolean isYes = false;
        boolean succeed = false;
        while(!succeed)
        {
            System.out.println ("Is this a serial number? yes/no (y / n)");
            String input = keyboard.nextLine();
            if(input.equals("y")||input.equals("yes"))
            {
                isYes = true;
                succeed = true;

            }
            if(input.equals("n")||input.equals("no"))
            {
                isYes = false;
                succeed = true;
            }
        }
        return isYes;
    }

    public String inputName()
    {
        boolean succeed = false;
        String name = "";
        while(!succeed)
        {
            try
            {
                System.out.println("Enter name:  ");
                name = keyboard.nextLine();

                if(name.matches("[^a-zA-Z_]+"))
                {
                    throw new IllegalArgumentException();

                }
                else
                {
                    succeed = true;
                }

            }
            catch(IllegalArgumentException e)
            {
                System.out.println ("The name must only contain letters");
            }
        }
        return name;
    }

    /**
     *
     * @return
     */
    public String inputAddress()
    {
        // makes an object keyboard to have input from the screen
        System.out.println ("Enter Address");
        String address = keyboard.nextLine();
        return address;
    }

    /**
     *
     * @return
     */
    public String inputPhone()
    {
        // makes an object keyboard to have input from the screen
        System.out.println ("Enter phone number:");
        String phone = keyboard.nextLine();
        return phone;
    }

    /**
     *
     * @return
     */
    public int inputRegNr()
    {
        // makes an object keyboard to have input from the screen
        System.out.println ("Enter the register number:");
        int regNr = keyboard.nextInt();
        keyboard.nextLine();
        return regNr;
    }

    /**
     *
     * @return
     */
    public int inputCardNr()
    {
        // makes an object keyboard to have input from the screen
        System.out.println ("Enter the card number:");
        int cardNr = keyboard.nextInt();
        keyboard.nextLine();
        return cardNr;
    }

    /**
     *
     * @return
     */
    public String inputSaleDate()
    {
        System.out.println ("Enter date:");
        String saleDate = keyboard.nextLine();
        return saleDate;
    }

    /**
     *
     * @return
     */
    public String inputDate()
    {
        System.out.println ("Enter date:");
        String Date = keyboard.nextLine();
        return Date;
    }

    /**
     *
     * @return
     */
    public String inputStatus()
    {
        System.out.println(" Enter status: ");
        String Date = keyboard.nextLine();
        return Date;
    }
}